/*
 * 攀枝花学院二手图书拍卖平台
 * Design by 王伟学
 * Version :1.0
 */
package com.auction.system.service.impl.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auction.system.dao.user.AdminInfoDao;
import com.auction.system.domain.dto.user.AdminInfo;
import com.auction.system.domain.query.user.AdminInfoQuery;
import com.auction.system.service.user.IAdminInfoService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * admin_info - 管理员信息表 Service实现类
 * @author 软件工程3班-王伟学
 */
@Service("adminInfoService")
public class AdminInfoServiceImpl implements IAdminInfoService {

    
    @Autowired
    private AdminInfoDao adminInfoDao;

	private final int QUERY_LIMLIT = 10000;

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public Long add(AdminInfo record) {
		record.setCreateTime(new Date());
        if(adminInfoDao.add(record)==1){
			return record.getId();
		}
		return 0L;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean edit(AdminInfo record) {
		record.setCreateTime(adminInfoDao.get(record.getId()).getCreateTime());
        return adminInfoDao.update(record)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean delete(Long id) {

        return adminInfoDao.delete(id)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AdminInfo get(Long id) {

        return adminInfoDao.get(id);
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AdminInfoQuery query(AdminInfoQuery query) {
        int count =  adminInfoDao.count(query);
        query.setCount(count);
        query.setData(adminInfoDao.query(query));
        return query;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public List<AdminInfo> getList(AdminInfoQuery query) {
        int count = adminInfoDao.count(query);
        int pags = (count - 1) / QUERY_LIMLIT + 1;
        List<AdminInfo> adminInfoList = new ArrayList<>(count);
        for(int i = 1 ;i <= pags ; i++ ){
            query.setPage(i);
            query.setLimit(QUERY_LIMLIT);
            adminInfoList.addAll(adminInfoDao.query(query));
        }
        return adminInfoList;
    }
}
